package smartlifev30.com.sdk.api.gateway_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.ConstantGateway;
import smartlifev30.com.sdk.bean.request.AddSceneRequest;
import smartlifev30.com.sdk.bean.request.DelSceneRequest;
import smartlifev30.com.sdk.bean.request.EditSceneRequest;
import smartlifev30.com.sdk.bean.request.ExeSceneRequest;
import smartlifev30.com.sdk.bean.request.SceneQueryRequest;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.bean.response.SceneQueryResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.baiwei.ResponseHelper;

/**
 * Created by tys on 2018/8/13.
 */

public class SceneMgmtGateway implements ISceneMgmtGateway {


    @Override
    public void queryScene(@NonNull String userName, @NonNull String token, @NonNull String sn,
                           @Nullable List<Integer> sceneId, @NonNull CallBack<SceneQueryResponse> callBack) {
        SceneQueryRequest sceneQueryRequest = new SceneQueryRequest();
        sceneQueryRequest.setFrom(userName);
        sceneQueryRequest.setTo(sn);
        sceneQueryRequest.setMsg_id(callBack.getMsgId());
        sceneQueryRequest.setMsg_class(ConstantGateway.MSG_CLASS_SCENE);
        sceneQueryRequest.setMsg_name("scene_query");
        sceneQueryRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        List<SceneQueryRequest.SceneList> list = null;
        if (sceneId != null) {
            list = new ArrayList<>();
            for (Integer id : sceneId) {
                list.add(sceneQueryRequest.new SceneList(id));
            }
        }
        sceneQueryRequest.setScene_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(sceneQueryRequest));
        ResponseHelper.addListen(callBack);
    }


    @Override
    public void addScene(@NonNull String userName, @NonNull String token, @NonNull String sn,
                         @Nullable AddSceneRequest.Scene scene, @NonNull CallBack<LoginResponse> callBack) {
        AddSceneRequest addSceneRequest = new AddSceneRequest();
        addSceneRequest.setFrom(userName);
        addSceneRequest.setTo(sn);
        addSceneRequest.setMsg_id(callBack.getMsgId());
        addSceneRequest.setMsg_class(ConstantGateway.MSG_CLASS_SCENE);
        addSceneRequest.setMsg_name("scene_add");
        addSceneRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        addSceneRequest.setToken(token);
        addSceneRequest.setScene(scene);
        NetManager.getInstance().sendMessage(JsonUtil.toString(addSceneRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void delScene(@NonNull String userName, @NonNull String token, @NonNull String sn,
                         @Nullable List<Integer> sceneId, @NonNull CallBack<LoginResponse> callBack) {
        DelSceneRequest delSceneRequest = new DelSceneRequest();
        delSceneRequest.setFrom(userName);
        delSceneRequest.setTo(sn);
        delSceneRequest.setMsg_id(callBack.getMsgId());
        delSceneRequest.setMsg_class(ConstantGateway.MSG_CLASS_SCENE);
        delSceneRequest.setMsg_name("scene_del");
        delSceneRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        delSceneRequest.setToken(token);
        List<DelSceneRequest.SceneList> list = null;
        if (sceneId != null) {
            list = new ArrayList<>();
            for (Integer id : sceneId) {
                list.add(delSceneRequest.new SceneList(id));
            }
        }
        delSceneRequest.setScene_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(delSceneRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void editScene(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull EditSceneRequest.Scene scene, @NonNull CallBack<LoginResponse> callBack) {
        EditSceneRequest editSceneRequest = new EditSceneRequest();
        editSceneRequest.setFrom(userName);
        editSceneRequest.setTo(sn);
        editSceneRequest.setMsg_id(callBack.getMsgId());
        editSceneRequest.setMsg_class(ConstantGateway.MSG_CLASS_SCENE);
        editSceneRequest.setMsg_name("scene_edit");
        editSceneRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        editSceneRequest.setToken(token);
        editSceneRequest.setScene(scene);
        NetManager.getInstance().sendMessage(JsonUtil.toString(editSceneRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void exeScene(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull Integer sceneId, @NonNull CallBack<LoginResponse> callBack) {
        ExeSceneRequest exeSceneRequest = new ExeSceneRequest();
        exeSceneRequest.setFrom(userName);
        exeSceneRequest.setTo(sn);
        exeSceneRequest.setMsg_id(callBack.getMsgId());
        exeSceneRequest.setMsg_class(ConstantGateway.MSG_CLASS_SCENE);
        exeSceneRequest.setMsg_name("scene_exe");
        exeSceneRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        exeSceneRequest.setToken(token);
        exeSceneRequest.setScene(exeSceneRequest.new Scene(sceneId));
        NetManager.getInstance().sendMessage(JsonUtil.toString(exeSceneRequest));
        ResponseHelper.addListen(callBack);
    }


}
